Conversation
| List<Thread> waiters = createAndStartWaiters(config.NUMBER_OF_WAITERS, orders, kitchen, statistic); | ||
|
|
||
| var startTime = System.nanoTime(); | ||
| List<Thread> students = createAndStartStudents(config.NUMBER_OF_STUDENTS, spoons, statistic, orders); |
There was a problem hiding this comment.
Лабу я конечно уже принял со скидкой, но тем не менее -- голые потоки как будто не стоит использовать. особенно когда у нас такой большой выбор экзекутор сервисов...
There was a problem hiding this comment.
Спасибо
С экзекьютором и правда было бы красивее
| private final Statistic statistic; | ||
| private final Spoon firstSpoon; | ||
| private final Spoon secondSpoon; | ||
| private final BlockingQueue<CompletableFuture<SoupOrderStatus>> orders; |
There was a problem hiding this comment.
На счет приоритета мы уже конечно пообщались, но если оставить это в стороне, то почему blocking? Почему не cuncurrent?
There was a problem hiding this comment.
Я хотел, чтобы consumer имел ожидание, тк в корутине или виртуальном треде, он может "заснуть", неблокирующая очередь сразу возвращает значение, значит цикл будет бежать без остановки
Вообще у меня давно в голове лежит знание, что неблокирующая очередь для producer/consumer не очень подходит, знание было взято на 2м курсе отсюда
baeldung примерно тоже самое пишет
On the other hand, if our consumer ends up waiting in a loop, we should probably choose a blocking queue as a better alternative.
There was a problem hiding this comment.
+Java заявляет, что ConcurrentLinkedQueue wait-free
This implementation employs an efficient "wait-free" algorithm based on one described in [Simple, Fast, and Practical Non-Blocking and Blocking Concurrent Queue Algorithms](http://www.cs.rochester.edu/u/michael/PODC96.html) by Maged M. Michael and Michael L. Scott.
но я этому не верю, в оригинальной статье, авторы называют свой алгоритм именно lock-free (возможен starvation, если тред постоянно проигрывает cas)
chatgpt, также считает
The ConcurrentLinkedQueue in Java is not strictly wait-free, but it is lock-free for most operations
| try { | ||
| log.debug("Student {} took spoon with id: {}", id, firstSpoon.getId()); | ||
| secondSpoon.lock(); | ||
| try { |
There was a problem hiding this comment.
Жутковатый каскад try catch, в будущем лучше делать поаккуратнее. Из интереса можете глянуть соседние PR
Описание решения доступно в README.md
Автор: Лысенко Артем Русланович